package com.cswnode.Config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;

/**
 * @author csw
 * @create 2020/10/1 18:45
 */
//配置redis
@Configuration
public class RedisConfig {
    @Bean
    public RedisTemplate<String,Object> redisTemplate (RedisConnectionFactory factory){
        //实例化bean
        RedisTemplate<String,Object> template = new RedisTemplate<>();
        //设置工厂，使template具备访问数据的能力
        template.setConnectionFactory(factory);
        //设置key序列化方式
        template.setKeySerializer(RedisSerializer.string());
        //设置value序列化方式
        template.setValueSerializer(RedisSerializer.json());
        //设置Hash的Key序列化方式
        template.setHashKeySerializer(RedisSerializer.string());
        //设置Hash的Value的序列化方式
        template.setHashValueSerializer(RedisSerializer.json());
        //使设置的值生效
        template.afterPropertiesSet();
        return template;
    }
}
